package com.example.cv.mapper;

import com.example.cv.model.Candidate;
import com.example.cv.model.Employer;
import com.example.cv.model.Position;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * @author 周杰
 */
@Mapper
public interface EmployerMapper {
    /**
     * description:用人单位查询
     * @param employer
     * @return com.example.cv.model.Employer
     */
    @Select("SELECT * FROM tb_employer WHERE account=#{account} and password=#{password}")
    Employer findEmployer(Employer employer) ;

    /**
     * description:用人单位注册（插入）
     * @param employer
     * @return boolean
     */
    @Insert("insert into tb_employer values(#{e_id},#{account},#{password},#{name},#{address},#{email},#{etb_time},#{legal_rep},#{phone_number},#{profile})")
    boolean addEmployer(Employer employer);

    /**
     * description:查询所有求职者
     * @param
     * @return java.util.List<com.example.cv.model.Candidate>
     */
    @Select("select * from tb_candidate where release_time is not null order by release_time desc")
    List<Candidate> findAllCandidates();

    /**
     * description:用人单位根据喜好动态查询求职者
     * @param candidate
     * @return java.util.List<com.example.cv.model.Employer>
     */
    @SelectProvider(type= EmployerMapperProvider1.class,method = "findCandidatesByParams")
    List<Candidate> findCandidatesByParams(Candidate candidate);

    class EmployerMapperProvider1{
        public String findCandidatesByParams(Candidate candidate){
            return new SQL(){
                {
                    SELECT("*");
                    FROM("tb_candidate");
                    WHERE("release_time is not null");
                    if(candidate.getMajor()!=""){
                        AND();
                        WHERE("major=#{major}");
                    }
                    if(candidate.getSalary_requirement()!=""){
                        AND();
                        WHERE("salary_requirement=#{salary_requirement}");
                    }
                    if(candidate.getRelease_time()!=""){
                        AND();
                        WHERE("release_time=#{release_time}");
                    }
                    if(candidate.getSchool()!=""){
                        AND();
                        WHERE("school=#{school}");
                    }
                }
            }.toString();
        }
    }

    /**
     * description:根据id查询用人单位
     * @param e_id
     * @return com.example.cv.model.Employer
     */
    @Select("select * from tb_employer where e_id=#{e_id}")
    Employer findEmployerById(int e_id);

    /**
     * description:发布简历
     * @param position
     * @return boolean
     */
    @Update("update tb_position set release_time=#{release_time} where p_id=#{p_id}")
    boolean updatePosReleaseTime(Position position);

    /**
     * description:撤销发布简历
     * @param position
     * @return boolean
     */
    @Update("update tb_position set release_time=#{release_time} where p_id=#{p_id}")
    boolean deletePosReleaseTime(Position position);

    /**
     * description:更新用人单位企业信息
     * @param employer
     * @return boolean
     */
    @Update("update tb_employer set password=#{password},name=#{name},address=#{address},email=#{email},etb_time=#{etb_time},legal_rep=#{legal_rep},phone_number=#{phone_number},profile=#{profile} where e_id=#{e_id}")
    boolean updateEmployer(Employer employer);

    @Select("select * from tb_candidate where expected_position in (select pos_name from tb_position where e_id=#{e_id})")
    List<Candidate> findRecommendCandidates(int e_id);

    @Select("select * from tb_position where e_id=#{e_id}")
    List<Position> findAllPositionsByEId(int e_id);

    @SelectProvider(type= EmployerMapperProvider2.class,method = "findPositionsByName")
    List<Position> findPositionsByName(int e_id, String pos_name);

    class EmployerMapperProvider2{
        public String findPositionsByName(int e_id, String pos_name){
            return new SQL(){
                {
                    SELECT("*");
                    FROM("tb_position");
                    WHERE("e_id='"+e_id+"'");
                    if(pos_name!=""){
                        AND();
                        WHERE("pos_name='"+pos_name+"'");
                    }
                }
            }.toString();
        }
    }

    @Insert("insert into tb_position values(#{p_id},#{e_id},#{e_name},#{address},#{pos_name},#{job_requir},#{salary},#{release_time},#{spec_address},#{pos_info},#{comp_info})")
    boolean addPosition(Position position);

    @Select("select * from tb_position where p_id=#{p_id}")
    Position findPositionByPId(int p_id);

    @Update("update tb_position set e_name=#{e_name},address=#{address},pos_name=#{pos_name},job_requir=#{job_requir},salary=#{salary},release_time=#{release_time},spec_address=#{spec_address},pos_info=#{pos_info},comp_info=#{comp_info} where p_id=#{p_id}")
    boolean updatePosition(Position position);

    @Delete("delete from tb_position where p_id=#{p_id}")
    boolean deletePosition(int p_id);
}
